package com.saascloud.config;

import com.obs.services.BasicObsCredentialsProvider;
import com.obs.services.ObsClient;
import com.obs.services.ObsConfiguration;
import com.saascloud.config.properties.FileProperties;
import com.saascloud.config.properties.obs.ObsProperties;
import com.saascloud.core.FileTemplate;
import com.saascloud.core.obs.ObsFileTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;

/**
 * @Author: lidingyi
 * @Contact: one_dream@aliyun.com
 * @Date: 2022/5/20 11:31
 * @Version: 1.0
 * @Description:
 */
@EnableConfigurationProperties(FileProperties.class)
public class ObsAutoConfiguration {

    @Autowired
    private FileProperties fileProperties;

    @Bean
    @Primary
    @ConditionalOnMissingBean(FileTemplate.class)
    @ConditionalOnProperty(name = "file.obs.enable", havingValue = "true")
    public FileTemplate ossTemplate() {
        return new ObsFileTemplate();
    }


    @Bean
    @ConditionalOnProperty(name = "file.obs.enable", havingValue = "true")
    public ObsClient obsClient() {
        ObsProperties obs = fileProperties.getObs();
        ObsConfiguration obsConfiguration = new ObsConfiguration();
        obsConfiguration.setEndPoint(obs.getEndPoint());
        obsConfiguration.setSocketTimeout(obs.getConnectionTimeout());
        obsConfiguration.setConnectionTimeout(obs.getSocketTimeOut());
        obsConfiguration.setMaxErrorRetry(obs.getMaxErrorRetry());
        return new ObsClient(new BasicObsCredentialsProvider(obs.getAk(), obs.getSk()), obsConfiguration);
    }


}
